package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.v1.Fault;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.FaultService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by k2data on 17-11-14.
 */
@RestController
@RequestMapping("/fault")
public class FaultController {

    private final FaultService faultService;

    @Autowired
    public FaultController(FaultService faultService){
        this.faultService=faultService;
    }

    @ApiOperation(value = "添加故障信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fault", value = "fault", required = true, dataType = "Fault", paramType = "body")
    })
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> addFault(@RequestBody Fault fault)throws Exception{
        return ResponseHelper.success(faultService.addFault(fault));
    }

    @ApiOperation(value = "修改故障信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "fault", value = "fault", required = true, dataType = "Fault", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putFault(@PathVariable("id") Long id,
                                                         @RequestBody Fault fault) throws Exception{
        fault.setId(id);
        return ResponseHelper.success(faultService.updateFault(fault));
    }

    @ApiOperation(value = "获取故障信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> getFault(@PathVariable("id") Long id)throws Exception{
        return ResponseHelper.success(faultService.getFault(id));
    }

    @ApiOperation(value = "获取故障信息列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "parentId", value = "parent id", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "isTree", value = "是否返回树形结构", dataType = "Boolean", paramType = "query")
    })
    @GetMapping(value = "/faults")
    public ResponseEntity<CommonResultResponse> getFaultList(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                             @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                             @RequestParam(value = "parentId", required = false) Long parentId,
                                                             @RequestParam(value = "isTree", required = false) Boolean isTree)throws Exception {
        List<Fault> list = faultService.getFaultList(parentId,isTree);
        pageSize = 20000;
        pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
        PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
        PageInfo pageInfo = new PageInfo(pageNum, pageSize, list.size());
        Fault fault = new Fault();
        fault.setFaultName("故障信息");
        fault.setId(0L);
        fault.setChildren(list);
        List<Fault> list1 = new ArrayList<>();
        list1.add(fault);

        return ResponseHelper.success(list1,pageInfo);
    }

    @ApiOperation(value = "删除故障信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
    })
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> delFault(@PathVariable("id") Long id)throws Exception{
        return ResponseHelper.success(faultService.delFault(id));
    }
}
